home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Unix / CNews / Source / conf / setnewsids.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-27  |  2.1 KB  |  93 lines

  1. /*
  2.  * setnewsids - sets ids to news/news, execs relay/relaynews.  Should be
  3.  *    setuid-root.  also add NEWSPERMS to the environment.
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <pwd.h>
  8. #include <grp.h>
  9. #include <sys/types.h>
  10.  
  11. #include "news.h"
  12. #include "libc.h"
  13. #include "config.h"
  14.  
  15. #ifndef RELAYNEWS
  16. #define RELAYNEWS binfile("relay/relaynews")
  17. #endif
  18. #ifndef NEWSUSER
  19. #define NEWSUSER "news"
  20. #endif
  21. #ifndef NEWSGROUP
  22. #define NEWSGROUP "news"
  23. #endif
  24.  
  25. char *progname;
  26.  
  27. static int userealids = NO;
  28.  
  29. /*
  30.  * main - parse arguments and handle options
  31.  */
  32. main(argc, argv)
  33. int argc;
  34. char *argv[];
  35. {
  36.     extern int optind;
  37.     extern char *optarg;
  38.  
  39.     progname = argv[0];
  40.  
  41.     /* setuid daemon prelude; various precautions */
  42.     (void) umask(newsumask());
  43.     (void) alarm(0);    /* cancel any pending alarm */
  44.     /*
  45.      * Reset certain environment variables to sane values.
  46.      */
  47.     if (putenv("PATH=/bin:/usr/bin") ||
  48.         putenv("IFS= \t\n"))
  49.         exit(1);
  50.     closeall(1);        /* closes all but std descriptors */
  51.     stdfdopen();        /* ensure standard descriptors are open */
  52.  
  53.     setids();        /* change of real and effective ids */
  54.     /* we are now running as news, so you can all relax */
  55.  
  56.     if (putenv("NEWSPERMS="))    /* avoid loops with this marker */
  57.         exit(1);
  58.     execv(RELAYNEWS, argv);    /* re-run relaynews */
  59.     error("can't exec %s", RELAYNEWS);
  60. }
  61.  
  62. setids()            /* change of real and effective ids */
  63. {
  64.     int newsuid = getuid(), newsgid = getgid();    /* default to real ids */
  65.  
  66.     (void) ctlfile((char *)NULL);    /* trigger unprivileged(), set userealids */
  67.     if (!userealids) {
  68.         register struct passwd *pwp;
  69.         register struct group *grp;
  70.  
  71.         pwp = getpwnam(NEWSUSER);
  72.         if (pwp != NULL) {
  73.             newsuid = pwp->pw_uid;
  74.             newsgid = pwp->pw_gid;
  75.         }
  76.         (void) endpwent();
  77.         grp = getgrnam(NEWSGROUP);
  78.         if (grp != NULL)
  79.             newsgid = grp->gr_gid;
  80.         (void) endgrent();
  81.     }
  82.     if (setgid(newsgid) < 0 || setuid(newsuid) < 0 ||
  83.         getgid() != newsgid || getuid() != newsuid)    /* xenix workaround */
  84.         error("set[ug]id failed", "");
  85.     /* we are now running as news, so you can all relax */
  86. }
  87.  
  88. void
  89. unprivileged()            /* called if NEWSARTS, NEWSCTL or NEWSBIN present */
  90. {
  91.     userealids = YES;
  92. }
  93.